﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Web;
using JYH.Business;
using JYH.Data;
using JYH.DataAccess;
using JYH.Framework;
using JYH.Model;

namespace JYH.Web.MES
{
    public class AuthenticationPage:ViewStateCompressPage
    {
        #region 登录用户信息

      
        private static Dictionary<string, SysUserInfo> __userCollection = new Dictionary<string, SysUserInfo>();
        private Database db = null;
        public Database myDatabase
        {
            get
            {
                return db;
            }
            set
            {
                db = value;
            }
        }


        /// <summary>
        /// 当前登录用户的身份信息
        /// </summary>
        public SysUserInfo CurrentUser
        {
            get
            {
                var __userID = "";
                if (Page != null)
                {
                    if (Page.User != null)
                    {
                        __userID = Page.User.Identity.Name;

                        // Language = Page.Response.Cookies.Get("Language").Value;

                        if (string.IsNullOrEmpty(__userID))
                            __userID = CookieHelper.ReadCookie();
                        if (!__userCollection.ContainsKey(__userID))
                        {
                            try
                            {
                                if (ConfigHelper.CheckConnectionStringKey("DBCONN"))
                                {
                                    myDatabase = new Database("DBCONN");
                                }
                                Sql sql = new Sql();
                                sql.Append("Select * From SysUserInfo");
                                sql.Append("Where SysUserInfo.UserNO='"+StringHelper.ReplaceSQLChar(__userID)+"'");
                                var user = myDatabase.SingleOrDefault<SysUserInfo>(sql.SQL, null);
                                if (user != null)
                                {
                                    RemoveCacheKey(__userID);

                                    __userCollection.Add(__userID, user);

                                }
                                else
                                {
                                    return null;
                                }

                            }
                            catch
                            {
                                return null;
                            }


                        }
                    }
                }

                return __userCollection[__userID];
            }
        }
        /// <summary>
        /// 当前登录用户
        /// </summary>
        public static SysUserInfo UserInfo
        {
            get
            {
                return new AuthenticationPage().CurrentUser;

            }
        }

        /// <summary>
        /// 删除指定的用户信息
        /// </summary>
        /// <param name="acctID"></param>
        public static void RemoveCacheKey(string acctID)
        {
            if (__userCollection != null && __userCollection.ContainsKey(acctID))
            {
                __userCollection.Remove(acctID);
            }
        }
        #endregion

    }
}
